package wechat.handler.response.base;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;


public abstract class XmlBase {
	
	protected Document document = null;
	protected Element parent;
	
	public XmlBase(String str) throws DocumentException{
		 this.document = DocumentHelper.parseText(str);
	}
	
	public XmlBase(){
		parent = DocumentHelper.createElement("xml");
	}
	
	
	protected String getNodeText(String xpath) {
		Node nameNode = this.document.selectSingleNode(xpath);
		if (nameNode == null) {
			return "";
		}
		return nameNode.getText().trim();
	}
	
	protected void addNodeValue(String nodeName, String nodeValue) throws Exception {
		if (nodeValue == null) {
			nodeValue = "";
		}
		if (nodeName == null) {
			return;
		}
		parent.addElement(nodeName).addCDATA(nodeValue);
	}
	
	protected Element addNode(String nodeName) throws Exception {
		if (nodeName == null) {
			return null;
		}
		return parent.addElement(nodeName);
	}

	
	
	protected abstract void transform() throws Exception;
	protected abstract String buildXml() throws Exception;
	
}
